import os
from typing import List, Dict
from agent.generate_image import batch_generate_images
from agent.generate_image_describe import generate_descriptions
from agent.insert_image_article import insert_images_into_article
from utils.md_to_docs import md_to_docx
# -------------------------- 配置参数（用户修改） --------------------------
ARTICLE_PATH_1 = "Emergency-Jewelry-Matching-Fix-5-Steps-to-Rescue-Styling-Disasters.md"  # 本地HTML文章路径
ARTICLE_PATH_2 = "Age-Appropriate-Jewelry-Matching-Checklist-Choices-for-Your-20s,-30s,-and-40+.md"
ARTICLE_PATH_3 = "Jewelry-Matching-Taboo-Checklist-Combinations-to-Never-Try.md"
# ARTICLE_PATH_4 = "10-Common-Jewelry-Matching-Mistakes-&-How-to-Avoid-Them.md"
# ARTICLE_PATH_5 = "Travel-Jewelry-Matching-Checklist-Lightweight-Yet-Stylish.md"
ARTICLE_PATHS = [ARTICLE_PATH_1, ARTICLE_PATH_2, ARTICLE_PATH_3]


# -------------------------- 函数调用流程 --------------------------
def main(ARTICLE_PATH):
    print("=" * 60)
    print("📌 开始文章图片插入流程")
    print(f"文章路径：{os.path.abspath(ARTICLE_PATH)}")
    print("=" * 60)

    try:
        # 步骤1：生成图片描述列表
        print("\n【步骤1】调用generate_descriptions生成图片描述...")
        image_descriptions: List[str] = generate_descriptions(ARTICLE_PATH)

        if not image_descriptions:
            raise ValueError("未生成任何图片描述，流程终止")

        print(f"✅ 成功生成{len(image_descriptions)}个图片描述：")
        for i, desc in enumerate(image_descriptions, 1):
            print(f"   {i}. {desc[:50]}...")

        # 步骤2：根据描述生成图片
        print("\n【步骤2】调用batch_generate_images生成图片...")
        prompt_image_pairs: List[Dict[str, str]] = batch_generate_images(image_descriptions)
        print(prompt_image_pairs)
        if not prompt_image_pairs:
            raise ValueError("未生成任何图片，流程终止")

        print(f"✅ 成功生成{len(prompt_image_pairs)}张图片：")
        # for i, pair in enumerate(prompt_image_pairs, 1):
        #     print(f"   {i}. 描述：{pair['prompt'][:30]}...")
        #     print(f"      路径：{pair['image_path']}")

        # 步骤3：插入图片到文章
        print("\n【步骤3】调用insert_images_to_local插入图片...")
        result = insert_images_into_article(
            ARTICLE_PATH,
            prompt_image_pairs,
        )
        if result:
            print(f"✅ 图片插入完成")

        # 步骤4：转word文档
        md_to_docx(ARTICLE_PATH)
        print(f"✅ MD转DOCX完成")


        # 输出最终结果
        print("\n" + "=" * 60)
        print("🎉 全部流程完成！")
        # print(f"最终文件路径：{result['save_path']}")
        # print(f"插入图片数量：{len(prompt_image_pairs)}")
        # print("=" * 60)

        return

    except Exception as e:
        print(f"\n❌ 流程失败：{str(e)}")
        return {"status": "failed", "error": str(e)}


# -------------------------- 执行入口 --------------------------
if __name__ == "__main__":
    # 验证文章文件存在性
    # if not os.path.exists(ARTICLE_PATH):
    #     print(f"错误：文章文件不存在 - {os.path.abspath(ARTICLE_PATH)}")
    # else:
    for article in ARTICLE_PATHS:
        main(article)


